package com.mark.web.bill.controller;

import com.github.pagehelper.PageInfo;
import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.web.bill.entity.from.AddGoodsFrom;
import com.mark.web.bill.entity.from.EditGoodsFrom;
import com.mark.web.bill.entity.po.Goods;
import com.mark.web.bill.entity.vo.GoodsDetailVo;
import com.mark.web.bill.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 记账模块的加工零件控制
 * @Author: Mark
 * @CreateDate: 2019/2/2 22:39
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "账单-加工零件模块")
@RestController
@RequestMapping(value = "/bill/goods/")
public class GoodsController {

    private final IGoodsService iGoodsService;

    @Autowired
    public GoodsController(IGoodsService iGoodsService) {
        this.iGoodsService = iGoodsService;
    }

    @ApiOperation(value = "添加加工零件")
    @PostMapping(value = "add")
    public ServerResponse<String> add(@Valid AddGoodsFrom addGoodsFrom, BindingResult result,
                                      @RequestParam(value = "goodsImage",required = false)MultipartFile goodsImage){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(addGoodsFrom,goods);
        int currentUserId = CurrentUser.getCurrentUserId();
        goods.setFkOwnerId(currentUserId);
        return iGoodsService.add(goods,goodsImage);
    }

    @ApiOperation(value = "编辑加工零件")
    @PostMapping(value = "edit")
    public ServerResponse<String> Edit(@Valid EditGoodsFrom editGoodsFrom, BindingResult result,
                                       @RequestParam(value = "goodsImage",required = false)MultipartFile goodsImage){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(editGoodsFrom,goods);
        int currentUserId = CurrentUser.getCurrentUserId();
        goods.setFkOwnerId(currentUserId);
        return iGoodsService.edit(goods,goodsImage);
    }

    @ApiOperation(value = "获取加工零件名称和对应ID")
    @GetMapping(value = "getOptionInfo")
    public ServerResponse<List<Map<String, Object>>> getOptionInfo(Integer categoryId){
        return iGoodsService.getOptionInfo(categoryId);
    }

    @ApiOperation(value = "分页筛选加工零件列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前的页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",defaultValue = "12")
    })
    @PostMapping(value = "getGoods")
    public ServerResponse<PageInfo<GoodsDetailVo>> getAccountList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                                  @RequestParam(value = "pageSize",defaultValue = "12") int pageSize,
                                                                  Integer categoryId,String searchGoodsName){
        return iGoodsService.getGoodsList(pageNum,pageSize,CurrentUser.getCurrentUserId(),categoryId,searchGoodsName);
    }

}
